package com.hongdee.scm.purchaseOrder.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.common.service.impl.Common;
import com.hongdee.scm.purchaseOrder.dao.ITScmPurchaseOrderDao;
import com.hongdee.scm.purchaseOrder.entity.TScmPurchaseOrder;
import com.hongdee.scm.purchaseOrder.service.ITScmPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmPurchaseOrderServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.purchaseOrder.service.impl
 * 作  者: 术洪超
 * 日  期:2017-04-27 16:55
 */
@Service
public class TScmPurchaseOrderServiceImpl implements ITScmPurchaseOrderService {

    private String tabName = "T_SCM_PURCHASEORDER";

    @Autowired
    private ITScmPurchaseOrderDao itScmPurchaseOrderDao;

    /**
     * @param data 要保存的采购单的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */

    @Override
    public TScmPurchaseOrder saveOrUpdate(TScmPurchaseOrder data, String uid) throws Exception {

        //id为空时则为添加
        if (data.getId() == null) {
            if (data.getOrderCode() != null) {
                if (existCode(data.getOrderCode())) {
                    throw new Exception("该采购单已经存在！");
                }
            }
            data.setCreateUser(uid);
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());

        } else {
            //id不为空时则为修改
            data.setUpdateUser(uid);
            data.setUpdateTime(Calendar.getInstance().getTimeInMillis());

        }
        data.setCompanyId(TokenUtil.getCompanyId(uid));
        this.itScmPurchaseOrderDao.saveOrUpdate(data);
        return data;
    }

    /**
     * @param uid   登录用户 ID
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  参数数组
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */

    @Override
    public Map page(Map data, String start, String limit, String uid) throws Exception {

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select * from " + tabName + " p where p.company_id = ?");
        params.add(TokenUtil.getCompanyId(uid));

        //判断采购单位
        if (data.get("buyUnit") != null && !data.get("buyUnit").toString().equals("")) {
            sql.append(" and p.buy_unit like ?");
            params.add(data.get("%" + "buyUnit" + "%"));
        }
        //判断采购品牌
        if (data.get("buyBrand") != null && !data.get("buyBrand").toString().equals("")) {
            sql.append(" and p.buy_brand like ?");
            params.add(data.get("%" + "buyBrand" + "%"));
        }
        return this.itScmPurchaseOrderDao.page(sql.toString(), params.toArray(), start, limit);
    }

    /**
     * @param uid   登录用户 ID
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @Override
    public Map info(String id, String uid) throws Exception {

        List params = new ArrayList();
        String sql = "select * from " + tabName + " p where id = ? and p.company_id= ?";
        params.add(id);
        params.add(TokenUtil.getCompanyId(uid));

        return this.itScmPurchaseOrderDao.info(sql, params.toArray());
    }

    /*
        判断采购单编码是否存在，用于保存时判断采购单编码是否重复
         */
    private boolean existCode(String orderCode) throws Exception {

        StringBuffer sb = new StringBuffer();
        sb.append("select * from " + tabName + " p ");

        List params = new ArrayList();
        Common common = new Common();
        Boolean hasWhere = false;
        if (!orderCode.isEmpty() && !orderCode.equals("")) {
            hasWhere = common.appendWhereIfNeed(sb, hasWhere);
            sb.append(" p.order_code = ? ");
            params.add(orderCode);
        }

        Map map = this.itScmPurchaseOrderDao.page(sb.toString(), params.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;

    }


}
